package com.ruoyi.web.controller.services;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.services.domain.ComplaintOnline;
import com.ruoyi.services.service.IComplaintOnlineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 在线投诉Controller
 * 
 * @author ruoyi
 * @date 2023-10-20
 */
@RestController
@RequestMapping("/services/complaintOnline")
public class ComplaintOnlineController extends BaseController
{
    @Autowired
    private IComplaintOnlineService complaintOnlineService;

    /**
     * 查询在线投诉列表
     */
    @PreAuthorize("@ss.hasPermi('services:complaintOnline:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComplaintOnline complaintOnline)
    {
        startPage();
        List<ComplaintOnline> list = complaintOnlineService.selectComplaintOnlineList(complaintOnline);
        return getDataTable(list);
    }

    /**
     * 导出在线投诉列表
     */
    @PreAuthorize("@ss.hasPermi('services:complaintOnline:export')")
    @Log(title = "在线投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComplaintOnline complaintOnline)
    {
        List<ComplaintOnline> list = complaintOnlineService.selectComplaintOnlineList(complaintOnline);
        ExcelUtil<ComplaintOnline> util = new ExcelUtil<ComplaintOnline>(ComplaintOnline.class);
        util.exportExcel(response, list, "在线投诉数据");
    }

    /**
     * 获取在线投诉详细信息
     */
    @PreAuthorize("@ss.hasPermi('services:complaintOnline:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(complaintOnlineService.selectComplaintOnlineById(id));
    }

    /**
     * 新增在线投诉，每天投诉上限次数5条
     */
    @PreAuthorize("@ss.hasPermi('services:complaintOnline:add')")
    @Log(title = "在线投诉", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComplaintOnline complaintOnline)
    {
        ComplaintOnline complaintOnlineQuery = new ComplaintOnline();
        //设置时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //格式化时间
        String format = simpleDateFormat.format(new Date());
        //获取base实体类对象来传递投诉时间
        complaintOnlineQuery.getParams().put("restrictTime",format);
        List<ComplaintOnline> complaintOnlineList = complaintOnlineService.selectComplaintOnlineList(complaintOnlineQuery);
        //判断投诉次数
        if(complaintOnlineList.size()>5){
            throw new ServiceException("今日投诉已达上限，请明天再试", HttpStatus.COMPLAINTS_ARE_CAPPED);
        }
        return toAjax(complaintOnlineService.insertComplaintOnline(complaintOnline));
    }

    /**
     * 修改在线投诉
     */
    @PreAuthorize("@ss.hasPermi('services:complaintOnline:edit')")
    @Log(title = "在线投诉", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComplaintOnline complaintOnline)
    {
        return toAjax(complaintOnlineService.updateComplaintOnline(complaintOnline));
    }

    /**
     * 删除在线投诉
     */
    @PreAuthorize("@ss.hasPermi('services:complaintOnline:remove')")
    @Log(title = "在线投诉", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(complaintOnlineService.deleteComplaintOnlineByIds(ids));
    }
}
